package com.ruoyi.examation.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.examation.vo.ExamVo;
import com.ruoyi.examation.vo.ExamClassExamVo;
import com.ruoyi.examation.service.IExamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 考试信息Controller
 *
 * @author Tellsea
 * @date 2025-09-28
 */
@Api(tags = "考试信息Controller")
@RestController
@RequestMapping("/examation/examation")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class ExamController extends BaseController {

    private final IExamService examService;

    @ApiOperation("查询考试信息列表")
    @PreAuthorize("@ss.hasPermi('examation:examation:list')")
    @GetMapping("/list")
    public TableDataInfo<ExamVo> list(ExamVo entity) {
        return examService.queryList(entity);
    }

    @ApiOperation("查询考试信息所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(ExamVo entity) {
        return AjaxResult.success("查询成功", examService.queryAll(entity));
    }

    @ApiOperation("导出考试信息列表")
    @PreAuthorize("@ss.hasPermi('examation:examation:export')")
    @Log(title = "考试信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamVo entity) {
        List<ExamVo> list = examService.queryAll(entity);
        ExcelUtil<ExamVo> util = new ExcelUtil<>(ExamVo.class);
        util.exportExcel(response, list, "考试信息数据");
    }

    @ApiOperation("获取考试信息详细信息")
    @PreAuthorize("@ss.hasPermi('examation:examation:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", examService.queryById(id));
    }

    @ApiOperation("新增考试信息")
    @PreAuthorize("@ss.hasPermi('examation:examation:add')")
    @Log(title = "考试信息", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody ExamVo entity) {
        return toAjax(examService.save(entity));
    }

    @ApiOperation("修改考试信息")
    @PreAuthorize("@ss.hasPermi('examation:examation:edit')")
    @Log(title = "考试信息", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody ExamVo entity) {
        return toAjax(examService.updateById(entity));
    }

    @ApiOperation("删除考试信息")
    @PreAuthorize("@ss.hasPermi('examation:examation:remove')")
    @Log(title = "考试信息", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(examService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    @ApiOperation("查询考试关联的班次列表")
    @GetMapping("/classes/{examId}")
    public TableDataInfo<ExamClassExamVo> getExamClasses(@PathVariable("examId") Long examId, ExamClassExamVo entity) {
        return examService.queryExamClassList(examId, entity);
    }

    @ApiOperation("新增考试班次关系")
    @Log(title = "新增考试班次关系", businessType = BusinessType.INSERT)
    @PostMapping("/addClass/{examId}")
    public AjaxResult addExamClass(@PathVariable("examId") Long examId, @RequestBody List<Long> classIds) {
        if (classIds == null || classIds.isEmpty()) {
            return AjaxResult.error("班次ID列表不能为空");
        }
        int successCount = examService.addExamClasses(examId, classIds);
        return AjaxResult.success("成功添加 " + successCount + " 个班次关系", successCount);
    }

    @ApiOperation("移除考试班次关系")
    @Log(title = "移除考试班次关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeClass/{examId}/{classId}")
    public AjaxResult removeExamClass(@PathVariable("examId") Long examId, @PathVariable("classId") Long classId) {
        return toAjax(examService.removeExamClass(examId, classId));
    }

    @ApiOperation("分页获取我关联的班次的考试列表")
    @GetMapping("/myClassExams")
    public TableDataInfo<ExamVo> getMyClassExams(ExamVo entity) {
        Long userId = getUserId();
        return examService.queryMyClassExamList(userId, entity);
    }
    
}
